package sz.szsmk.citizencard.retrofitrxjava;/*
 * Copyright 2016. chenshufei
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.Context;

import rx.functions.Action1;
import sz.szsmk.citizencard.dialog.NetRequestTipsDialog;
import sz.szsmk.citizencard.model.network.bean.response.BaseBean;

/**
 * <br /> author: chenshufei
 * <br /> date: 16/1/6
 * <br /> email: chenshufei2@sina.com
 */
public abstract class SubscribeResult<T extends BaseBean> implements Action1<T> {

    private Context mContext;
    private String mMsg;
    private NetRequestTipsDialog mNetRequestTipsDialog;

    public SubscribeResult() {

    }

    public SubscribeResult(Context context) {
        this.mContext = context;
//        StartDialog();
    }

    private void StartDialog() {
        if (showDialog() && null != mContext) {
            if (null == mNetRequestTipsDialog) {
                mNetRequestTipsDialog = new NetRequestTipsDialog();
            }
            if (!mNetRequestTipsDialog.isAdded()) {
                FragmentTransaction ft = ((Activity) mContext).getFragmentManager().beginTransaction();
                ft.add(mNetRequestTipsDialog, this.getClass().getSimpleName());
                ft.commitAllowingStateLoss();
            }
            mNetRequestTipsDialog.setCancelable(false);
        }
    }

    public SubscribeResult(Context context, String msg) {
        this.mContext = context;
        this.mMsg = msg;
    }

    public boolean showDialog() {
        return true;
    }

    @Override
    public void call(T t) {
        if ("000000".equals(t.getCode()) || "0000".equals(t.getCode())) {
            onOk(t);
        } else if ("000001".equals(t.getCode())) {
            onNext(t);
        } else if ("000004".equals(t.getCode()) || "000002".equals(t.getCode())|| "000016".equals(t.getCode())) {
            onUnlogin(t);
        } else if("000014".equals(t.getCode())){
            onFailure(t);
        } else{
            onFailure(t);
        }
        dismissLoadingDialog();
    }

    protected void onFailure(T response) {

    }

    protected abstract void onOk(T response);

    protected void onNext(T response) {

    }

    protected void onUnlogin(T response) {

    }


    private void dismissLoadingDialog() {
        if (null != mNetRequestTipsDialog) {
            mNetRequestTipsDialog.dismissAllowingStateLoss();
            mNetRequestTipsDialog = null;
        }
    }

}
